package com.maidada.codesharebackend.common;

import com.maidada.codesharebackend.exception.ErrorCode;
import com.maidada.codesharebackend.exception.GlobalErrorCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 通用返回结果
 *
 * @author wulinxuan
 * @date 2025-06-24 11:30
 */
@Data
public class CommonResult<T> implements Serializable {

    /**
     * 错误码
     */
    private Integer code;

    /**
     * 数据
     */
    private T data;

    /**
     * 消息
     */
    private String msg;

    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<>(GlobalErrorCode.SUCCESS.getCode(), data, GlobalErrorCode.SUCCESS.getMessage());
    }

    public static <T> CommonResult<T> error(String msg) {
        return error(GlobalErrorCode.SYSTEM_ERROR, msg);
    }

    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        return error(errorCode, errorCode.getMessage());
    }

    public static <T> CommonResult<T> error(int code, String msg) {
        return new CommonResult<>(code, null, msg);
    }

    public static <T> CommonResult<T> error(ErrorCode errorCode, String msg) {
        return new CommonResult<>(errorCode.getCode(), null, msg);
    }

    public CommonResult(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }
}
